package com.mcr.graduateprogram.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.graduateprogram.bean.Games;
import com.mcr.graduateprogram.common.R;
import com.mcr.graduateprogram.service.IGamesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author graduateprogram
 * @since 2023-02-25
 */
@Api(tags = "游戏相关接口")
@CrossOrigin
@RestController
@RequestMapping("/graduateprogram/games")
public class GamesController {
    @Autowired
    private IGamesService iGamesService;
    @GetMapping("getPage")
    @ApiOperation(value = "分页查询游戏")
    public R getPage(@RequestParam(value = "page")Integer page,
                     @RequestParam(value = "pageSize")Integer pageSize,
                     @RequestParam(value = "classesId",required = false)Long classesId){
        Page<Games> gamesPage=iGamesService.getPage(page,pageSize,classesId);
        return R.success(gamesPage);
    }
    @ApiOperation(value = "根据id删除单个游戏")
    @GetMapping("delById")
    @CacheEvict(value = "graduationProgram",key = "'allGames'")//删缓存allGames
    public R delById(@RequestParam(value = "id")Long id){
        Boolean t=iGamesService.delById(id);
        return R.success(t?"删除成功":"删除失败，该游戏存在相应版本仍是上架状态，请把该游戏所有版本全部下架后重试");
    }
    @ApiOperation(value = "根据ids批量删除游戏")
    @GetMapping("delByIds")
    @CacheEvict(value = "graduationProgram",key = "'allGames'")//删缓存allGames
    public R delByIds(@RequestParam(value = "ids")List<Long> ids){
        Boolean t=iGamesService.deByIds(ids);
        return R.success(t?"删除成功":"删除失败，这些游戏存在相应版本仍是上架状态，请把这些游戏的所有版本全部下架后重试");
    }
    @ApiOperation(value = "添加游戏")
    @PostMapping("addNew")
    @CacheEvict(value = "graduationProgram",key = "'allGames'")//删缓存allGames
    public R addNew(@RequestBody Games games){
        iGamesService.save(games);
        return R.success(null);
    }
    @ApiOperation(value = "根据id修改游戏数据")
    @PostMapping("updateById")
    @CacheEvict(value = "graduationProgram",key = "'allGames'")//删缓存allGames
    public R updateById(@RequestBody Games games){
        iGamesService.updateById(games);
        return R.success(null);
    }
    @ApiOperation(value = "查询所有游戏")
    @GetMapping("getAll")
    @Cacheable(value = "graduationProgram",key = "'allGames'")//从缓存取数据或存缓存allGames
    public R getAll(){
        return R.success(iGamesService.list());
    }
}
